﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using Catering.View_Operaction;
using System.Text;
using System.IO;

namespace Catering
{
    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application excel1 = null;
            Microsoft.Office.Interop.Excel._Worksheet ws = null;
            Microsoft.Office.Interop.Excel.Range canbie = null;
            string stick = DateTime.Now.Ticks.ToString();

            try
            {
                excel1 = new Microsoft.Office.Interop.Excel.Application();
                excel1.Application.Workbooks.Add(true);//引用Excel工作簿
                //Microsoft.Office.Interop.Excel._Worksheet ws = new Microsoft.Office.Interop.Excel.WorksheetClass();
                ws = new Microsoft.Office.Interop.Excel.WorksheetClass();
                ws = (Microsoft.Office.Interop.Excel._Worksheet)excel1.ActiveSheet;//获取当前活动工作薄
                //Microsoft.Office.Interop.Excel.Range canbie;//创建Range操作一行作为标题
                excel1.Visible = false;

                #region  表头

                #endregion

                #region 第一行标题

                ws.Cells[1, 1] = "菜品名称";
                ws.Cells[1, 2] = "大类";
                ws.Cells[1, 3] = "细类";
                ws.Cells[1, 4] = "特点";
                ws.Cells[1, 5] = "做法";
                ws.Cells[1, 6] = "原料";

                #endregion

                #region 主要内容

                int h = 2;//餐别行数
                int maxl = 1;
                DataTable dt = new Catering_Rules.T_Dish().GetAllList().Tables[0];
                foreach (DataRow item in dt.Rows)
                {
                    string id = item["ID"].ToString();
                    string name = item["Name"].ToString();
                    int dtid = Convert.ToInt32(item["DTID"]);
                    string tedian = item["Characteristic"].ToString();
                    string zuofa = item["Method"].ToString();
                    string typesname = "";
                    string typename = "";
                    DataTable d = new Catering_Rules.T_DishInfo().GetList(" DID=" + id).Tables[0];
                    Catering_Model.DishTypes ds = new Catering_Rules.DishTypes().GetModel(dtid);
                    if (ds != null)
                    {
                        typesname = ds.DTName;
                        Catering_Model.DishType dm = new Catering_Rules.DishType().GetModel(ds.DTID);
                        if (dm != null) typename = dm.DT;
                    }

                    ws.Cells[h, 1] = typename;
                    ws.Cells[h, 2] = typesname;
                    ws.Cells[h, 3] = name;
                    ws.Cells[h, 4] = tedian;
                    ws.Cells[h, 5] = zuofa;
                    string yl = "";
                    foreach (DataRow t in d.Rows)
                    {
                        string rawid = t["RawID"].ToString();
                        string num = t["Content"].ToString();
                        string rawname = t["DName"].ToString();
                        Catering_Model.E_RawMaterial rm = new Catering_Rules.T_RawMaterial().GetModel(Convert.ToInt32(rawid));
                        if (rm != null && !string.IsNullOrEmpty(rm.RAWName)) rawname = rm.RAWName;
                        yl += (rawname + num) + ",";
                    }
                    ws.Cells[h, 6] = yl.TrimEnd(',');
                    h++;
                }
                ws.Cells.EntireColumn.AutoFit();
                canbie = ws.get_Range(ws.Cells[1, 1], ws.Cells[h - 1, 6]);
                canbie.Borders.LineStyle = 1;

                #endregion

                #region 底部

                #endregion

                if (!Directory.Exists(Server.MapPath("~/xls_template"))) Directory.CreateDirectory(Server.MapPath("~/xls_template"));
                ws.SaveAs(Server.MapPath("~/xls_template") + "\\" + stick + ".xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlAddIn8);

            }
            finally
            {
                if (excel1 != null)
                {
                    if (ws != null)
                    {
                        if (canbie != null)
                        {
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(canbie);
                            canbie = null;

                        }
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
                        ws = null;
                    }
                }
                excel1.Application.Workbooks.Close();
                excel1.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excel1);
                Toolss.KillExcel(excel1);
                excel1 = null;
                GC.Collect();
            }

            #region 发送

            try
            {
                string path = Server.MapPath("~/xls_template") + "\\" + stick + ".xls";
                System.IO.FileInfo file = new System.IO.FileInfo(path);
                Response.Clear();
                Response.Charset = "GB2312";
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                // 添加头信息，,弹出另存为窗口  
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
                //让浏览器头显示下载信息  
                Response.AddHeader("Content-Length", file.Length.ToString());
                // 指定返回一个不能被客户端读取的流，下载  
                Response.ContentType = "application/ms-excel";
                //把文件流下载到客户端  
                Response.WriteFile(file.FullName);
                // 停止页面的执行
                Response.End();
            }
            catch { }

            #endregion
        }

    }
}